#ifndef _AST_H_
#define _AST_H_

#include <vector>
#include <string>

using namespace std;

#include "Token.h"

class AST {
private:
    Token *token;
    vector<AST> children;
public:
    AST() {
        token = nullptr;
    }
    AST(Token *token) {
        this->token = token;
    }
    AST(int token_type) {
        this->token = new Token(token_type);
    }

    int get_node_type() {
        return token->type;
    }
    void add_child(AST t) {
        children.push_back(t);
    }
    bool is_null() {
        return token == nullptr;
    }

    string to_string() {
        return token != nullptr ? token->to_string() : "null";
    }
    string to_string_tree() {
        if (children.size() == 0)
            return this->to_string();
        string buf;
        if (!is_null()) {
            buf.push_back('(');
            buf += to_string();
            buf.push_back(' ');
        }
        for (int i = 0; i < children.size(); i++) {
            AST *t = &children[i];
            if (i > 0)
                buf.push_back(' ');
            buf += t->to_string_tree();
        }
        if (!is_null())
            buf.push_back(')');
        return buf;
    }
};

#endif
